package com.kmxd.ams.app.arrange.command;

import cn.easyes.core.conditions.select.LambdaEsQueryWrapper;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.ImmutableSet;
import com.kmxd.ams.client.archive.IArchiveOrderFieldSvc;
import com.kmxd.ams.client.archive.dto.ArchiveOrderFieldQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveOrderFieldListVO;
import com.kmxd.ams.client.search.dto.CatalogSearchDTO;
import com.kmxd.ams.core.common.constant.ArchConst;
import com.kmxd.ams.core.common.constant.EsConst;
import com.kmxd.ams.core.common.enums.FieldOrderTypeEnum;
import com.kmxd.ams.infra.arrange.base.SystemBO;
import java.util.List;
import lombok.AllArgsConstructor;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;

/**
 * 档案通用查询操作
 *
 * @author nickbi
 */
@Component
@AllArgsConstructor
public class CommonQueryCmd {
  private final IArchiveOrderFieldSvc archiveOrderFieldSvc;

  /**
   * 通用排序设置
   *
   * @param req
   * @param wrapper
   */
  public void buildFieldOrderQuery(SystemBO req, QueryWrapper<? extends SystemBO> wrapper) {
    ArchiveOrderFieldQueryDTO qryOrder = new ArchiveOrderFieldQueryDTO();
    qryOrder.setArchTypeId(req.getArchTypeId());
    qryOrder.setTableId(req.getTableId());
    List<ArchiveOrderFieldListVO> orderFieldList = archiveOrderFieldSvc.selectList(qryOrder);
    orderFieldList.forEach(
        item -> {
          String orderFieldCode = item.getOrderFieldCode();
          wrapper.orderBy(
              ObjectUtil.isNotEmpty(orderFieldCode),
              FieldOrderTypeEnum.ASC.getType().equals(item.getOrderType()),
              orderFieldCode);
        });
  }

  public <T> void buildEsFieldOrderQuery(CatalogSearchDTO req, LambdaEsQueryWrapper<T> wrapper) {
    ArchiveOrderFieldQueryDTO qryOrder = new ArchiveOrderFieldQueryDTO();
    qryOrder.setArchTypeId(req.getArchTypeId());
    qryOrder.setTableId(req.getTableId());
    List<ArchiveOrderFieldListVO> orderFieldList = archiveOrderFieldSvc.selectList(qryOrder);
    orderFieldList.forEach(
        item -> {
          String orderFieldCode = item.getOrderFieldCode();
          String field =
              StringUtils.isCamel(orderFieldCode)
                  ? orderFieldCode
                  : StringUtils.underlineToCamel(orderFieldCode);
          wrapper.orderBy(
              ObjectUtil.isNotEmpty(orderFieldCode),
              FieldOrderTypeEnum.ASC.getType().equals(item.getOrderType()),
              field);
        });
  }

  public <T> void buildEsFieldOrderQuery(CatalogSearchDTO req, SearchSourceBuilder query) {
    ArchiveOrderFieldQueryDTO qryOrder = new ArchiveOrderFieldQueryDTO();
    qryOrder.setArchTypeId(req.getArchTypeId());
    qryOrder.setTableId(req.getTableId());
    List<ArchiveOrderFieldListVO> orderFieldList = archiveOrderFieldSvc.selectList(qryOrder);
    orderFieldList.forEach(
        item -> {
          String orderFieldCode = item.getOrderFieldCode();
          String field =
              StringUtils.isCamel(orderFieldCode)
                  ? orderFieldCode
                  : StringUtils.underlineToCamel(orderFieldCode);
          if (ImmutableSet.of(ArchConst.DOC_SEQUENCE,ArchConst.VOL_SEQUENCE).contains(field)){
              field = field+EsConst.FIELD_NUMBER_SUFFIX;
          }
          SortOrder sortOrder =
              FieldOrderTypeEnum.ASC.getType().equals(item.getOrderType())
                  ? SortOrder.ASC
                  : SortOrder.DESC;
          // text类型排序，添加keywrod后缀
          if (ImmutableSet.of(ArchConst.ARCH_CODE, ArchConst.FIELD_TITLE).contains(field)) {
            field = field + EsConst.FIELD_KEYWORD_SUFFIX;
          }
          query.sort(field, sortOrder);
        });
  }
}
